/**
 * 表单字段远程搜索的方法mixins
 */

import { debounce, trim } from '@/libs/tools'

import {
    queryCustomForCustGrid
} from '@/api/customer' // 海关编码查询

/**
 * 查询单位
 * v: 查询内容
 * params: 查询参数
 * len: 长度触发查询条件
 * cb: 回调函数
 */

const queryCustomer = debounce(function (v, params, len, cb) {
    let value = trim(v)
    if (!value || value.length < len) {
        return
    }
    return queryCustomForCustGrid({
        ...params,
        custLocation: '境内企业',
        pageNum: 1,
        pageSize: 20
    }).then(res => {
        let list = []
        if (res.data.code === '0000') {
            const data = res.data.data || {}
            list = (data.list || []).map(item => {
                return {
                    operatorId: item.custId,
                    operatorName: item.custName,
                    operatorCode: item.customsCode,
                    operatorCiq: item.ciqCode,
                    coScc: item.socialCreditCode
                }
            })
        }
        cb && cb(list)
    })
}, 200)
// 查询境外收货人方法
const searchOverseas = debounce(function (v, params, cb) {
    return queryCustomForCustGrid({
        ...params,
        custLocation: '境外企业',
        pageNum: 1,
        pageSize: 20
    }).then(res => {
        let list = []
        if (res.data.code === '0000') {
            const data = res.data.data || {}
            list = (data.list || []).map(item => {
                return {
                    overseasId: item.custId,
                    overseasCode: item.socialCreditCode,
                    overseasName: item.custNameEn
                }
            })
        }
        cb && cb(list)
    })
}, 200)

// 生产销售单位和消费使用单位 远程搜索方法
export const ownerRemote = {
    methods: {
        // 生产销售单位和消费使用单位
        handleSearchOwnerScc (v, cb) {
            return queryCustomer(v, { socialCreditCode: v }, 5, cb)
        },
        handleSearchOwnerCode (v, cb) {
            return queryCustomer(v, { customsCode: v }, 5, cb)
        },
        handleSearchOwnerCiq (v, cb) {
            return queryCustomer(v, { ciqCode: v }, 5, cb)
        },
        handleSearchOwnerName (v, cb) {
            return queryCustomer(v, { custName: v }, 3, cb)
        }
    }
}

// 境内外收发货人 远程搜索方法
export const tradeRemote = {
    methods: {
        handleSearchOperatorScc (v, cb) {
            return queryCustomer(v, { socialCreditCode: v }, 5, cb)
        },
        handleSearchOperatorCode (v, cb) {
            return queryCustomer(v, { customsCode: v }, 5, cb)
        },
        handleSearchOperatorCiq (v, cb) {
            return queryCustomer(v, { ciqCode: v }, 5, cb)
        },
        handleSearchOperatorName (v, cb) {
            return queryCustomer(v, { custName: v }, 3, cb)
        }
    }
}

// 查询境外收货人 by code
export const overseasRemote = {
    methods: {
        handleSearchOverseasCode (v, cb) {
            trim(v).length > 1 ? searchOverseas(v, { socialCreditCode: v }, cb) : cb([])
        },
        // 查询境外收货人 by name
        handleSearchOverseasName (v, cb) {
            trim(v).length > 1 ? searchOverseas(v, { custNameEn: v }, cb) : cb([])
        }
    }
}
